package com.micro.school.h5.service.impl;

import com.micro.school.h5.domin.StudentH5Login;
import com.micro.school.h5.mapper.StudentH5LoginMapper;
import com.micro.school.h5.service.StudentH5LoginService;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * (StudentH5Login)表服务实现类
 *
 * @author makejava
 * @since 2025-08-14 11:32:47
 */
@Service("studentH5LoginService")
public class StudentH5LoginServiceImpl implements StudentH5LoginService {

    @Resource
    private StudentH5LoginMapper studentH5LoginMapper;



    @Override
    public boolean loginStudentLogin(StudentInfos data, StudentInfoVos studentInfoVos) {
        StudentH5Login studentH5Login = new StudentH5Login();
        studentH5Login.setStudentName(studentInfoVos.getName());
        studentH5Login.setIdNumber(studentInfoVos.getIdNumber());
        studentH5Login.setTenantId(data.getTenantId());
        StudentH5Login studentH5Login1 = studentH5LoginMapper.getOneLoginStudent(studentH5Login);
        if (studentH5Login1 != null){
            //查询有值就登录
            if (!studentH5Login1.getOpenid().equals(studentInfoVos.getOpenId())){
                //不同openId需要删除后重新绑定
                studentH5LoginMapper.deleteById(studentH5Login1.getId(),data.getTenantId());
                studentH5Login1.setTenantId(data.getTenantId());
                studentH5Login1.setOpenid(studentInfoVos.getOpenId());
                studentH5Login1.setCreateTime(new Date());
                studentH5Login1.setStudentId(data.getId());
                studentH5Login1.setUpdateTime(new Date());
                studentH5Login1.setStudentName(studentInfoVos.getSchoolName());
                studentH5LoginMapper.insert(studentH5Login1);
                return true;
            }else{
                return true;
            }
        }else{
            //查询无值就绑定    此处查询是防止他换学校了
            StudentH5Login studentH5Login2 = new StudentH5Login();
            studentH5Login2.setStudentName(studentInfoVos.getName());
            studentH5Login2.setIdNumber(studentInfoVos.getIdNumber());
            StudentH5Login studentH5Login3 = studentH5LoginMapper.getOneLoginStudent(studentH5Login2);
            if (studentH5Login3 != null){
                //表示确实换学校了   需要重新绑定
                studentH5LoginMapper.delete(studentH5Login3.getId());
                studentH5Login2.setTenantId(data.getTenantId());
                studentH5Login2.setOpenid(studentInfoVos.getOpenId());
                studentH5Login2.setCreateTime(new Date());
                studentH5Login2.setUpdateTime(new Date());
                studentH5Login2.setStudentId(data.getId());
                studentH5Login2.setStudentName(studentH5Login3.getStudentName());
                studentH5LoginMapper.insert(studentH5Login2);
                return true;
            }else{
                //表示确实没有绑定过
                studentH5Login2.setTenantId(data.getTenantId());
                studentH5Login2.setOpenid(studentInfoVos.getOpenId());
                studentH5Login2.setCreateTime(new Date());
                studentH5Login2.setUpdateTime(new Date());
                studentH5Login2.setStudentId(data.getId());
                studentH5Login2.setStudentName(studentInfoVos.getName());
                studentH5LoginMapper.insert(studentH5Login2);
                return true;
            }

        }



    }

    @Override
    public List<StudentH5Login> queryLoginList(String openId) {
        StudentH5Login studentH5Login = new StudentH5Login();
        studentH5Login.setOpenid(openId);
        return studentH5LoginMapper.queryAllByLimit(studentH5Login);
    }

    @Override
    public void deleteLoginStudent(Integer id) {
        studentH5LoginMapper.delete(id);
    }
}
